package org.skatiger.jrove.component.html;

import java.io.IOException;

import org.skatiger.jrove.ResponseWriter;
import org.skatiger.jrove.ViewContext;
import org.skatiger.jrove.component.JroveAttr;
import org.skatiger.jrove.component.UIResource;
import org.skatiger.jrove.renderer.Html;
import org.skatiger.jrove.util.StringUtils;

public class HtmlStylesheet extends UIResource {

    @Override
    public boolean getRendersChildren() {
        return true;
    }
    @Override
    public void encodeChildren(ViewContext context) throws IOException {
        super.encodeChildren(context);

        ResponseWriter writer = context.getResponseWriter();
        String _value = getValue();
        boolean hasValue = StringUtils.isNotBlank(_value);
        if (hasValue) {
            writer.startElement(Html.LINK_ELEM, this);
            writer.writeAttribute(Html.ID_ATTR, getId(), JroveAttr.ID_ATTR);
            writer.writeAttribute(Html.REL_ATTR, Html.STYLESHEET_VALUE, null);
            writer.writeAttribute(Html.TYPE_ATTR, Html.STYLE_TYPE_TEXT_CSS,
                    null);
            String _href = UIResource.getResourceURL(context, _value);
            writer.writeAttribute(Html.HREF_ATTR, _href, JroveAttr.VALUE_ATTR);
            //        <link rel="stylesheet" type="text/css" href="theme.css" />
            writer.endElement(Html.LINK_ELEM);
        } else {
            writer.startElement(Html.STYLE_ELEM, this);
            super.encodeChildren(context);
            writer.endElement(Html.STYLE_ELEM);
        }

    }
}
